package com.android.common.mvp.view.fragment;

import android.view.ViewGroup;
import android.widget.Toast;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.android.common.R;
import com.android.common.adapter.holder.CommonTvHolder;
import com.android.common.mvp.model.CodeMsgBean;
import com.android.common.base.rv.BaseRvAdapter;
import com.android.common.base.rv.BaseRvFragment;
import com.android.common.base.rv.BaseRvHolder;
import com.android.common.constant.Constant;
import com.android.common.mvp.present.FilePresent;
import com.android.common.utils.media.MediaUtils;
import com.android.common.utils.media.screen.RecordScreenCallBack;
import com.android.common.utils.media.screen.ScreenHelper;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class FunctionTestFragment extends BaseRvFragment<String> {

    private boolean isRecordScreen = false;

    @Override
    protected BaseRvAdapter<String> initAdapter() {
        BaseRvAdapter<String> adapter = new BaseRvAdapter<String> (this.getContext()) {
            @Override
            protected BaseRvHolder<String> createDataViewHolder(ViewGroup parent, int viewType) {
                return new CommonTvHolder(mLayoutInflater.inflate(R.layout.item_common_tv, parent, false));
            }
        };
        adapter.setOnItemClickListener((view, position) -> {
            switch(position){
                case Constant.ZERO:
                    ScreenHelper.getInstance().captureScreenService(mActivity, new RecordScreenCallBack() {
                        @Override
                        public void onSucceed(Object obj, String methodName) {
                            super.onSucceed(obj, methodName);
                            ScreenHelper.getInstance().stopCaptureScreenService(null);
                            MediaUtils.saveImageToGallery(mActivity, obj.toString());
                            Toast.makeText(mActivity, obj.toString(), Toast.LENGTH_SHORT).show();
                        }

                        @Override
                        public void onFailed(CodeMsgBean responseBean, String methodName) {
                            super.onFailed(responseBean, methodName);
                            ScreenHelper.getInstance().stopCaptureScreenService(null);
                            Toast.makeText(mActivity, "failed", Toast.LENGTH_SHORT).show();
                        }
                    });
                    break;
                case Constant.ONE:
                    //开始录屏
                    if (isRecordScreen){
                        ScreenHelper.getInstance().stopRecordScreenService(new RecordScreenCallBack() {
                            @Override
                            public void onSucceed(Object obj, String methodName) {
                                isRecordScreen = false;
                                assert adapter.getData() != null;
                                adapter.getData().set(Constant.ONE,"开始录屏");
                                updateItem(Constant.ONE);
                                MediaUtils.saveVideoToGallery(mActivity, obj.toString());
                                Toast.makeText(mActivity, obj.toString(), Toast.LENGTH_SHORT).show();
                            }
                        });
                    }else {
                        ScreenHelper.getInstance().startRecordScreenService(mActivity, new RecordScreenCallBack() {
                            @Override
                            public void onStart(Object obj) {
                                isRecordScreen = true;
                                assert adapter.getData() != null;
                                adapter.getData().set(Constant.ONE,"结束录屏");
                                updateItem(Constant.ONE);
                            }
                        });
                    }
                    break;
                case Constant.TWO:
                    showProgress();
                    FilePresent uploadPresent = new FilePresent(this);
                    File file = new File("sdcard/123.png");
                    uploadPresent.uploadFile(file);
                    break;
                case Constant.THREE:
                    showProgress();
                    FilePresent downloadPresent = new FilePresent(this);
                    downloadPresent.downloadFile("123","sdcard/","123.png");
                    break;

            }
        });
        return adapter;
    }

    @Override
    public boolean onEnableRefresh() {
        return false;
    }

    @Override
    public boolean onEnableLoadMore() {
        return false;
    }

    @Override
    protected String onSetTitleText() {
        return getString(R.string.common_function_test);
    }

    @Override
    public RecyclerView.LayoutManager onSetRvLayoutManager() {
        return new GridLayoutManager(getContext(),
            3, GridLayoutManager.VERTICAL, false);
    }

    @Override
    protected void onLoadData() {
        super.onLoadData();
        List<String> data = new ArrayList<>();
        data.add("截屏");
        data.add("开始录屏");
        data.add("上传");
        data.add("下载");
        refreshOrLoadSuccess(data);
    }

    @Override
    public void onSucceed(Object obj, String methodName) {
        switch (methodName){
            case FilePresent.UPLOAD_FILE:
            case FilePresent.DOWN_LOAD_FILE:
                Toast.makeText(mActivity, "操作成功", Toast.LENGTH_SHORT).show();
                break;
        }
    }
}
